package io.modelcontextprotocol.sdk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟聊天
 */
public class ChatRecord {
    public static Map<String, Conversation> conversationsMap = new HashMap();

    static {
        List<Message> messages = new ArrayList<>();
        messages.add(new Message("1", "sender1", "你好! 我是sender1"));
        messages.add(new Message("2", "sender2", "你好! 我是sender2"));
        messages.add(new Message("3", "sender3", "你好! 我是sender3"));

        conversationsMap.put("1", new Conversation("1", "", messages));
        conversationsMap.put("2", new Conversation("2", "", messages));
        conversationsMap.put("3", new Conversation("3", "", messages));
        conversationsMap.put("4", new Conversation("4", "", messages));
    }


    public static List<Conversation> getAllConversation() {
        List<Conversation> conversations = new ArrayList<>();
        for (Conversation conversation : conversationsMap.values()) {
            conversations.add(conversation);
        }
        return conversations;
    }


    public static Conversation getConversationById(String conversationId) {
        return conversationsMap.get(conversationId);
    }


    public static List<Message> getMessageByConversationId(String conversationId) {
        Conversation conversation = conversationsMap.get(conversationId);
        if (conversation == null) return null;
        List<Message> messages = conversation.getMessages();
        return messages;
    }

    public static void sendMessage(String conversationId, String sender, String content) {
        Conversation conversation = conversationsMap.get(conversationId);
        if (conversation != null) {
            String messageId = String.valueOf(System.currentTimeMillis());
            // 确保字符串编码正确，避免乱码
            String safeSender = sender != null ? sender : "";
            String safeContent = content != null ? content : "";
            Message newMessage = new Message(messageId, safeSender, safeContent);
            conversation.getMessages().add(newMessage);
        }
    }

    static class Conversation {
        private String id;
        private String sender;
        private List<Message> messages;

        public Conversation(String id, String sender, List<Message> messages) {
            this.id = id;
            this.sender = sender;
            this.messages = messages;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getSender() {
            return sender;
        }

        public void setSender(String sender) {
            this.sender = sender;
        }

        public List<Message> getMessages() {
            return messages;
        }

        public void setMessages(List<Message> messages) {
            this.messages = messages;
        }
    }


    static class Message {
        private String id;
        private String sender;
        private String content;

        public Message(String id, String sender, String content) {
            this.id = id;
            this.sender = sender;
            this.content = content;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getSender() {
            return sender;
        }

        public void setSender(String sender) {
            this.sender = sender;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

}
